package com.kylin.system.controller;

import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.exception.ServiceException;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.core.validator.BeanValidator;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.system.entity.SysNotice;
import com.kylin.system.service.SysNoticeService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 公告 信息操作处理
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController
{
    @Autowired
    private SysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysNotice>> list(SysNotice notice)
    {
        // 构造搜索条件
        notice.getQueryWrapper()
                .like(StringUtils.isNotBlank(notice.getNoticeTitle()), "notice_title", notice.getNoticeTitle())
                .like(ObjectUtils.isNotEmpty(notice.getCreateBy()), "create_by", notice.getCreateBy())
                .eq(StringUtils.isNotBlank(notice.getNoticeType()), "notice_type", notice.getNoticeType());

        Page<SysNotice> page = new Page<>();
        page = noticeService.listPage(page, notice);
        return ResultBean.onSuccess(page);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public ResultBean<SysNotice> getInfo(@PathVariable Long noticeId)
    {
        Assert.isBlank("查询编号不能为空", noticeId);
        SysNotice item = noticeService.getById(noticeId);
        return ResultBean.onSuccess(item);
    }

    /**
     * 新增通知公告
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping
    public ResultBean<Object> save(@RequestBody SysNotice notice)
    {
        // 数据校验
        BeanValidator.validate(notice);
        noticeService.save(notice);
        return ResultBean.onSuccess();
    }

    /**
     * 修改通知公告
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean<Object> edit(@RequestBody SysNotice notice)
    {
        // 数据校验
        BeanValidator.validate(notice);
        noticeService.save(notice);
        return ResultBean.onSuccess();
    }

    /**
     * 删除通知公告
     */
//    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public ResultBean<Object> remove(@PathVariable Long[] noticeIds)
    {
        if(ObjectUtils.isEmpty(noticeIds)) {
            throw new ServiceException("请选择需要删除的数据");
        }

        int count = noticeService.deleteBatchIds(Arrays.asList(noticeIds));
        return ResultBean.onSuccess().put("count", count);
    }
}
